﻿using BigEgg.Framework.Foundation;
using System;
using System.ComponentModel.DataAnnotations;
using UnitTestDemo.Applications.Properties;

namespace UnitTestDemo.Applications
{
    public abstract class Animal : ValidationModel
    {
        #region Fields
        private readonly string animalType;

        private string name;
        private Gender gender;
        private int age;
        #endregion

        public Animal(string animalType, string name)
        {
            //  This is a bug. It should throw the ArgumentException instead of ArgumentNullException.
            if (string.IsNullOrWhiteSpace(animalType)) { throw new ArgumentNullException("animalType"); }
            if (string.IsNullOrWhiteSpace(name)) { throw new ArgumentException("name"); }

            this.animalType = animalType;
            this.gender = Applications.Gender.Male;
            this.name = name;
        }

        #region Properties
        public string AnimalType { get { return this.animalType; } }

        [Required(ErrorMessageResourceName = "NameMandatory", ErrorMessageResourceType = typeof(Resources))]
        [StringLength(50, ErrorMessageResourceName = "NameMaxLength", ErrorMessageResourceType = typeof(Resources))]
        public string Name 
        {
            get { return this.name; }
            set 
            {
                if (this.name != value)
                {
                    this.name = value;
                    RaisePropertyChanged("Name");
                }
            }
        }

        public Gender Gender
        {
            get { return this.gender; }
            set 
            {
                if (this.gender != value)
                {
                    this.gender = value;
                    RaisePropertyChanged("Gender");
                }
            }
        }

        [Range(1, int.MaxValue, ErrorMessageResourceName = "AgeEqualOrLarger", ErrorMessageResourceType = typeof(Resources))]
        public int Age
        {
            get { return this.age; }
            set 
            {
                if (this.age != value)
                {
                    this.age = value;
                    RaisePropertyChanged("Age");
                }
            }
        }
        #endregion


        #region Methods

        #region Public Methods
        public void Read(string document)
        {
            if (string.IsNullOrWhiteSpace(document)) { throw new ArgumentException("document"); }
            if (!CanRead()) { throw new NotSupportedException(); }

            ReadCore(document);
        }

        public virtual bool CanRead()
        {
            return false;
        }
        #endregion

        #region Protected Methods
        protected virtual void ReadCore(string document)
        {
            throw new NotImplementedException();
        }
        #endregion

        #region Private Methods

        #endregion

        #endregion

    }
}
